package com.gitee.magic.framework.base.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gitee.magic.core.json.JsonObject;
import com.gitee.magic.core.utils.StringUtils;

import io.swagger.v3.oas.annotations.media.Schema;

public class SchemaEnumUtils {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SchemaEnumUtils.class);
	
	public static List<String> get(Class<?> type) {
		List<String> names=new ArrayList<>();
		for(Field f:type.getFields()) {
			Schema property=f.getAnnotation(Schema.class);
			if(property!=null) {
				if(!StringUtils.isEmpty(property.title())) {
					names.add(property.title()+":"+f.getName());
				}else if(!StringUtils.isEmpty(property.description())) {
					names.add(property.description()+":"+f.getName());
				}
			}else {
				names.add(f.getName());
				if(LOGGER.isWarnEnabled()) {
					LOGGER.warn("未添加@Schema注解Class:{} Field:{}",type,f.getName());
				}
			}
		}
		return names;
	}
	
	public static String getString(Class<?> type,String v) {
		for (Field f : type.getFields()) {
			if (v.equals(f.getName())) {
				Schema property = f.getAnnotation(Schema.class);
				if (property != null) {
					if(!StringUtils.isEmpty(property.title())) {
						return property.title();
					}else if(!StringUtils.isEmpty(property.description())) {
						return property.description();
					}
				}
			}
		}
		return v;
	}
	
	public static JsonObject getJson(Class<?> type,String v) {
		for (Field f : type.getFields()) {
			if (v.equals(f.getName())) {
				Schema property = f.getAnnotation(Schema.class);
				if (property != null) {
					JsonObject json=new JsonObject();
					if(!StringUtils.isEmpty(property.title())) {
						json.put(v, property.title());
					}else if(!StringUtils.isEmpty(property.description())) {
						json.put(v, property.description());
					}
					return json;
				}
			}
		}
		return null;
	}

}
